SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


Alter  trigger tfb_fixastmodify_bd  on dbo.tfb_fixastmodify 
instead of delete
as   
begin   
  if @@rowcount<>1 return
  set nocount on
  declare  @ideproption integer;   
  declare  @iplanusemonth integer;   
  declare  @isumusedmonth integer;   
  declare  @dpplanworkvalue decimal (18,2);   
  declare  @dpsumworkedvalue decimal (18,2);   
  declare  @dprawamt decimal (18,2);   
  declare  @dpprice decimal (18,2);  
  declare  @iquantity decimal (18,8);  
  declare  @dpsumdepramt decimal (18,2);   
  declare  @dpremainamt decimal (18,2);   
  declare  @dpremainrate decimal (18,8);   
  declare  @dpmonthdepramt decimal (18,2);   
  declare  @dpmonthdeprrate decimal (18,8);   
  declare  @istatusid integer;   
  declare  @ideprmethodid integer;   
  declare  @iastsubid integer;   
  declare  @dprsubid integer;   
  declare  @workunit varchar(10);
  declare  @location varchar(64);
  declare  @old_faadjid int,
           @old_fixedassetid int,
           @old_modtypeid int;

  select @old_faadjid = faadjid,
         @old_fixedassetid = fixedassetid,
         @old_modtypeid = modtypeid
  from deleted;
  
  select @ideproption = deproption,
        @iplanusemonth = planusemonth,   
        @isumusedmonth = sumusedmonth,
        @dpplanworkvalue = planworkvalue,
        @dpsumworkedvalue = sumworkedvalue,   
        @dprawamt = rawamt,
        @dpprice = price, 
        @iquantity = quantity,  
        @dpsumdepramt = sumdepramt,
        @dpremainamt = remainamt,
        @dpremainrate = remainrate,   
        @dpmonthdepramt = monthdepramt,
        @dpmonthdeprrate = monthdeprrate,
        @istatusid = statusid,
        @ideprmethodid = deprmethodid,
        @iastsubid = astsubid ,
        @dprsubid = dprsubid,
        @workunit=workunit,
        @location=location
 from tfb_oldfixedassets    
 where asssetchangeid=@old_faadjid;
   
 if  (@dpremainrate is null) set @dpremainrate = 0;
 if  (@dpremainamt is null) set @dpremainamt = 0;
 if  (@dpsumdepramt is  null) set @dpsumdepramt = 0;
 update tfb_fixedassets set deproption=@ideproption,planusemonth=@iplanusemonth,sumusedmonth=@isumusedmonth,   
                          planworkvalue=@dpplanworkvalue,sumworkedvalue=@dpsumworkedvalue,rawamt=@dprawamt, 
		price = @dpprice,quantity=@iquantity,    
                          sumdepramt=@dpsumdepramt,remainamt=@dpremainamt,remainrate=@dpremainrate,   
                          monthdepramt=@dpmonthdepramt,monthdeprrate=@dpmonthdeprrate,statusid=@istatusid,   
                          deprmethodid=@ideprmethodid,astsubid=@iastsubid , dprsubid = @dprsubid, workunit=@workunit,location=@location
 where fixedassetid=@old_fixedassetid;   
   
   delete from tfb_oldfixedassets where asssetchangeid=@old_faadjid;   
   if (@old_modtypeid=2)    
      begin   
         delete from tfb_fixastdept where fixedassetid=@old_fixedassetid and used=1;   
         update tfb_fixastdept set used=1    
         	where fixedassetid=@old_fixedassetid    
         	and pcode in (select max(pcode) from tfb_fixastdept   
         			where fixedassetid=@old_fixedassetid);   
      end   
  delete tfb_fixastmodify from deleted old where tfb_fixastmodify.faadjid = old.faadjid;
end




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

